package com.compty.paint.domain;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.compty.paint.util.Constants;
import com.compty.security.domain.User;

/**
 * 拍卖预展
 * @author zhuqh
 */
@Entity
@Table(name = "t_auction")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Auction {
	
	private Long id;

	/************ 拍卖会名称 ****************/
	private String name;

	/************ 策划展 ****************/
	private String planningUnit;

	/************ 封面  图片****************/
	private ImageInfo imageInfo;
	
	/************ 封面拍品名称 ****************/
	private String coverName;
	
	/************ 开拍时间 ****************/
	private Date beginningTime;
	
	/************ 结束时间 ****************/
	private Date endTime;

	/************ 省份 ****************/
	private ProvinceInfo provinceInfo;

	/************ 城市 ****************/
	private CityInfo cityInfo;
	
	/************ 详细地址 ****************/
	private String address;

	/************ 联系人 ****************/
	private String contact;
	
	/************ 联系电话 ****************/
	private String tel;

	/************ 上传状态 ****************/
	private UploadState uploadState;
	
	/************ 审核状态 ****************/
	private AuditState auditState;
	
	/************ 上传时间 ****************/
	private Date uploadTime;

	/************ 最后修改时间 ****************/
	private Date modifyTime;

	/************ 录入人 ****************/
	private User entryPerson;
	
	/************ 拍卖像册集 ****************/
	private List<ImageInfo> imagesInfo = new ArrayList<ImageInfo>();
	
	/************ 作品总件数  ****************/
	private Integer totalCount = 0;
	
	private String currentStateFlag = "3";//当前进展状态，1:预展、2:进行中、3：已结束
	
	private String imagepath;//图片路径

	@Id
	@GeneratedValue
	@Column(name = "id", unique = true, nullable = false)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name="name",length=80)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "plan_unit",length = 100)
	public String getPlanningUnit() {
		return planningUnit;
	}

	public void setPlanningUnit(String planningUnit) {
		this.planningUnit = planningUnit;
	}

	@ManyToOne(fetch = FetchType.LAZY,cascade =  {CascadeType.REMOVE })
	@JoinColumn(name="image_id")
	public ImageInfo getImageInfo() {
		return imageInfo;
	}

	public void setImageInfo(ImageInfo imageInfo) {
		this.imageInfo = imageInfo;
	}

	@Column(name = "cover_name",length = 100)
	public String getCoverName() {
		return coverName;
	}

	public void setCoverName(String coverName) {
		this.coverName = coverName;
	}

	@Column(name = "begintime")
	public Date getBeginningTime() {
		return beginningTime;
	}

	public void setBeginningTime(Date beginningTime) {
		this.beginningTime = beginningTime;
	}
	
	@Column(name = "endtime")
	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="province_id")
	public ProvinceInfo getProvinceInfo() {
		return provinceInfo;
	}

	public void setProvinceInfo(ProvinceInfo provinceInfo) {
		this.provinceInfo = provinceInfo;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="city_id")
	public CityInfo getCityInfo() {
		return cityInfo;
	}

	public void setCityInfo(CityInfo cityInfo) {
		this.cityInfo = cityInfo;
	}

	@Column(name = "address",length = 200)
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Column(name = "contact",length = 40)
	public String getContact() {
		return contact;
	}

	public void setContact(String contact) {
		this.contact = contact;
	}

	@Column(name = "tel",length = 20)
	public String getTel() {
		return tel;
	}

	public void setTel(String tel) {
		this.tel = tel;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="uploadstate_id")
	public UploadState getUploadState() {
		return uploadState;
	}

	public void setUploadState(UploadState uploadState) {
		this.uploadState = uploadState;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="auditstate_id")
	public AuditState getAuditState() {
		return auditState;
	}

	public void setAuditState(AuditState auditState) {
		this.auditState = auditState;
	}

	@Column(name = "uploadtime")
	public Date getUploadTime() {
		return uploadTime;
	}

	public void setUploadTime(Date uploadTime) {
		this.uploadTime = uploadTime;
	}

	@Column(name = "modifytime")
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="userinfo_id")
	public User getEntryPerson() {
		return entryPerson;
	}

	public void setEntryPerson(User entryPerson) {
		this.entryPerson = entryPerson;
	}

	 @ManyToMany(cascade =  {CascadeType.REMOVE}
	    , fetch = FetchType.LAZY)
	    @JoinTable(name = "T_auction_imageinfo", joinColumns =  {
	        @JoinColumn(name = "auction_id")
	    }
	    , inverseJoinColumns =  {
	        @JoinColumn(name = "image_id")
	    }
	    )
	public List<ImageInfo> getImagesInfo() {
		return imagesInfo;
	}

	public void setImagesInfo(List<ImageInfo> imagesInfo) {
		this.imagesInfo = imagesInfo;
	}

	@Column(name = "total_count")
	public Integer getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}
	
	@Transient
	public String getCurrentStateFlag() {
		Date  currentDate = Calendar.getInstance().getTime();
		Date endTime = null;
		if(beginningTime != null){
			endTime = new Date(beginningTime.getTime()+ 7*3600*1000);
		}
		if( beginningTime != null){
			int flag = currentDate.compareTo(beginningTime);
			if(flag > 0){
				if( endTime != null){
					int flag2 = currentDate.compareTo(endTime);
					if( flag2 > 0){
						currentStateFlag = "3";
					}else{
						currentStateFlag = "2";
					}
				}else{
					currentStateFlag = "3";
				}
			}else{
				currentStateFlag = "1";
			}
		}else{
			if( endTime != null){
				int flag2 = currentDate.compareTo(endTime);
				if( flag2 > 0){
					currentStateFlag = "3";
				}else{
					currentStateFlag = "2";
				}
			}else{
				currentStateFlag = "3";
			}
		}
		return currentStateFlag;
	}

	public void setCurrentStateFlag(String currentStateFlag) {
		this.currentStateFlag = currentStateFlag;
	}
	
	@Transient
	public String getImagepath() {
		if( imageInfo!= null ){
			this.imagepath = Constants.IMAGE_SERVER+Constants.AuctionFlag+"/"+imageInfo.getId()+"."+imageInfo.getSuffix();
		}
		else{
			this.imagepath = Constants.IMAGE_SERVER ;
		}
		return imagepath;
	}
	
}
